﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PackagesInProcess
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        SalesManagerEntities _sme = new SalesManagerEntities();
        IEnumerable<tblPackage> gridView;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var status = (from c in _sme.codeInventoryTransactionStatus 
                          where (new string[] { "A", "P", "K" }).Contains( c.InventoryTransactionStatusCode )
                          select new { c.InventoryTransactionStatusCode, c.Name }).ToList();
            ((CollectionViewSource)FindResource("Status")).Source = status;
            cmbStatus.SelectedIndex = 0;

            var statusTo = (from c in _sme.codeInventoryTransactionStatus select new { c.InventoryTransactionStatusCode, c.Name }).ToList();
            statusTo.Insert(0, new { InventoryTransactionStatusCode = (string)null, Name = string.Empty });
            ((CollectionViewSource)FindResource("StatusTo")).Source = statusTo;
            cmbStatus2.SelectedIndex = 0;

            var Owner = (from c in _sme.tblOwner select new { c.OwnerID, c.LongName }).ToList();
            Owner.Insert(0, new { OwnerID = -1, LongName = "All" });
            ((CollectionViewSource)FindResource("Owners")).Source = Owner;
            cmbOwner.SelectedIndex = 0;

            LoadGrid();
            cmbOwner.SelectionChanged += cmbOwner_SelectionChanged;
            cmbStatus.SelectionChanged += cmbStatus_SelectionChanged;
            cmbStatus2.SelectionChanged += cmbStatus2_SelectionChanged;
        }

        private void cmbOwner_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadGrid();
        }

        private void cmbStatus_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadGrid();
        }

        private void cmbStatus_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            LoadGrid();
        }

        void LoadGrid()
        {
            // Load the grid with matching status codes and owners.
            Cursor = Cursors.Wait;
            var v = from c in _sme.tblPackage
                where ( from p in c.InventoryTransactions where p.InventoryTransactionStatusCode == (string)cmbStatus.SelectedValue select p ).Any()
                    && c.OwnerID == ((cmbOwner.SelectedIndex == 0) ? c.OwnerID : (Int32)cmbOwner.SelectedValue)
                select c;
            gridView = v.ToArray();
            ((CollectionViewSource)FindResource("Packages")).Source = gridView;
            txtCount.Text = gridView.Count().ToString();
            Cursor = Cursors.Arrow;
        }

        private void cmbStatus2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbStatus2.SelectedIndex == 0)
                return;

            if (gridView == null || gridView.Count() == 0 || gridView.Where(p => p.Checked).Count() == 0)
            {
                MessageBox.Show("No packages selected");
                cmbStatus2.SelectedIndex = 0;
                return;
            }

            if (MessageBox.Show("Are you sure you want to update these " + gridView.Where(p => p.Checked).Count().ToString() + " packages to the status " + cmbStatus2.Text + "?",
                "Update Package Status", MessageBoxButton.YesNoCancel) == MessageBoxResult.Yes)
            {
                foreach (tblPackage package in gridView.Where(p => p.Checked))
                {
                    foreach (tblInventoryTransaction transaction in package.InventoryTransactions)
                        transaction.InventoryTransactionStatusCode = (string)cmbStatus2.SelectedValue;
                }
            }
            Cursor = Cursors.Wait;
            try
            {
                _sme.SaveChanges();
            }
            catch (Exception err)
            {
                MessageBox.Show("Error saving status changes\n" + err.Message);
                return;
            }
            LoadGrid();
            cmbStatus2.SelectedIndex = 0;
        }

        private void chkAll_Checked(object sender, RoutedEventArgs e)
        {
            foreach (tblPackage package in gridView)
                package.Checked = true;
        }

        private void chkAll_Unchecked(object sender, RoutedEventArgs e)
        {
            foreach (tblPackage package in gridView)
                package.Checked = false;
        }

    }
}
